package com.sample.library.web;

import android.app.Activity;
import android.net.Uri;
import android.webkit.WebView;

import com.download.library.DownloadImpl;
import com.download.library.DownloadListenerAdapter;
import com.download.library.DownloadTask;
import com.download.library.Extra;
import com.download.library.ResourceRequest;
import com.just.agentweb.AbsAgentWebSettings;
import com.just.agentweb.AgentWeb;
import com.just.agentweb.DefaultDownloadImpl;
import com.just.agentweb.WebListenerManager;

public class AgentWebSettings extends AbsAgentWebSettings {

    private final Activity activity;

    private AgentWeb mAgentWeb;

    public AgentWebSettings(Activity activity) {
        this.activity = activity;
    }

    @Override
    protected void bindAgentWebSupport(AgentWeb agentWeb) {
        this.mAgentWeb = agentWeb;
    }

    /**
     * AgentWeb 4.0.0 内部删除了 DownloadListener 监听 ，以及相关API ，将 Download 部分完全抽离出来独立一个库，
     * 如果你需要使用 AgentWeb Download 部分 ， 请依赖上 compile 'com.download.library:Downloader:4.1.1' ，
     * 如果你需要监听下载结果，请自定义 AgentWebSetting ， New 出 DefaultDownloadImpl
     * 实现进度或者结果监听，例如下面这个例子，如果你不需要监听进度，或者下载结果，下面 setDownloader 的例子可以忽略。
     *
     * @param webView
     * @param downloadListener
     * @return WebListenerManager
     */
    @Override
    public WebListenerManager setDownloader(WebView webView, android.webkit.DownloadListener downloadListener) {
        return super.setDownloader(webView, new DefaultDownloadImpl(activity, webView, this.mAgentWeb.getPermissionInterceptor()) {

            @Override
            protected ResourceRequest<DownloadTask> createResourceRequest(String url) {
                return DownloadImpl.getInstance(activity)
                        .with(activity.getApplicationContext())
                        .url(url)
                        .quickProgress()
                        .addHeader("", "")
                        .setEnableIndicator(true)
                        .autoOpenIgnoreMD5()
                        .setRetry(5)
                        .setBlockMaxTime(100000L);
            }

            @Override
            protected void taskEnqueue(ResourceRequest resourceRequest) {
                resourceRequest.enqueue(new DownloadListenerAdapter() {
                    @Override
                    public void onStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength, Extra extra) {
                        super.onStart(url, userAgent, contentDisposition, mimetype, contentLength, extra);
                    }

                    @MainThread
                    @Override
                    public void onProgress(String url, long downloaded, long length, long usedTime) {
                        super.onProgress(url, downloaded, length, usedTime);
                    }

                    @Override
                    public boolean onResult(Throwable throwable, Uri path, String url, Extra extra) {
                        return super.onResult(throwable, path, url, extra);
                    }
                });
            }
        });
    }

}
